package com.pangda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.mapper.DictDataMapper;
import com.pangda.pojo.DictData;
import com.pangda.pojo.DictType;
import com.pangda.pojo.dto.DictDataDto;
import com.pangda.pojo.dto.DictPageParam;
import com.pangda.pojo.vo.DictDataVo;
import com.pangda.service.IDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pangda.service.IDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author Panda.No72
 * @since 2024-06-20
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {
    @Autowired
    private IDictTypeService dictTypeService;

    @Override
    public ResponseResult add(DictDataDto dictDataDto) {
        Integer dictId = dictTypeService.getOne(new LambdaQueryWrapper<DictType>().eq(DictType::getDictType, dictDataDto.getDictType())).getDictId();
        dictDataDto.setDictId(dictId);
        dictDataDto.setDictLabel(dictDataDto.getDictLabel().trim());
        dictDataDto.setCreateBy(UserContext.getUser());
        return ResponseResult.success(save(BeanUtil.toBean(dictDataDto, DictData.class)));
    }

    @Override
    public PageResult<DictDataVo> getList(DictPageParam param) {
        LambdaQueryWrapper<DictData> wrapper = new LambdaQueryWrapper<>();
        Page<DictData> page;
        if (BeanUtil.isNotEmpty(param)){
            wrapper.like(ObjectUtil.isNotEmpty(param.getDictLabel()), DictData::getDictLabel, param.getDictLabel());
            wrapper.like(ObjectUtil.isNotEmpty(param.getDictType()), DictData::getDictType, param.getDictType());
            wrapper.like(ObjectUtil.isNotEmpty(param.getStatus()), DictData::getStatus, param.getStatus());
            page = page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
        }else {
            page = page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
        }
        return PageResult.success(page, DictDataVo.class);
    }
}
